public class LinkedListSet<E> implements Set<E> {

    private LinkedList<E> list;
    public LinkedListSet() {
        list = new LinkedList<>();
    }

    @Override
    public void add(E elem) {
        if (!contains(elem)) { // contains 复杂度O(n)
            list.addFirst(elem);
        }
    }

    @Override
    public void remove(E elem) {
        list.remove(elem);
    }

    @Override
    public boolean contains(E elem) {
        return list.contains(elem);
    }

    @Override
    public int size() {
        return list.size();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("LinkedListSet: [");
        for (int i = 0; i < list.size(); i++) {
            res.append(list.get(i));
            if (i != list.size() - 1)
                res.append(", ");
        }
        res.append("]");
        return res.toString();
    }
}
